﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GerbilLib;

namespace MediaGerbil
{
    public partial class ImagePreview : UserControl
    {
        public PictureBox PictureBox
        {
            get { return pictureBox; }
            set { pictureBox = value; }
        }

        public TmdbImage SelectedImageSize
        {
            get
            {
                return (TmdbImage)cmbSizes.SelectedItem;
            }
        }

        private TmdbImage Image { get; set; }
        private TmdbImageSet ImageSet { get; set; }
        private TmdbImage.ImageSize DefaultImageSize { get; set; }

        public ImagePreview(TmdbImage image, TmdbImageSet imageSet, TmdbImage.ImageSize defaultSize)
        {
            Image = image;
            ImageSet = imageSet;
            DefaultImageSize = defaultSize;

            InitializeComponent();
            SetImageProperties();
            PopulateSizes();
        }

        private void SetImageProperties()
        {
            PictureBox.ImageLocation = Image.Url.ToString();
            int widthOffset = Width - PictureBox.Width;
            PictureBox.Width = Image.Width;
            Width = PictureBox.Width + widthOffset;
            int heightOffset = Height - PictureBox.Height;
            PictureBox.Height = Image.Height;
            Height = PictureBox.Height + heightOffset;
            PictureBox.BorderStyle = BorderStyle.FixedSingle;
        }

        private void PopulateSizes()
        {
            foreach (TmdbImage image in ImageSet.Images)
                cmbSizes.Items.Add(image);
            // Select default download size from Preferences
            switch(DefaultImageSize)
            {
                case TmdbImage.ImageSize.Cover: cmbSizes.SelectedItem = ImageSet.Cover; break;
                case TmdbImage.ImageSize.Mid: cmbSizes.SelectedItem = ImageSet.Mid; break;
                case TmdbImage.ImageSize.Original: cmbSizes.SelectedItem = ImageSet.Original; break;
                case TmdbImage.ImageSize.Poster: cmbSizes.SelectedItem = ImageSet.Poster; break;
                case TmdbImage.ImageSize.Thumb: cmbSizes.SelectedItem = ImageSet.Thumb; break;
            }
            if (cmbSizes.SelectedItem == null) cmbSizes.SelectedItem = ImageSet.Original;
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            this.OnClick(e);
        }

        private void cmbSizes_Click(object sender, EventArgs e)
        {
            this.OnClick(e);
        }
    }
}
